package com.controller;


import com.domain.User;
import com.github.pagehelper.PageInfo;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public String login(User user, HttpSession session) {
        User username = userService.login(user);
        if (username != null) {
            session.setAttribute("user", username);
            return "main";
        }
        return "redirect:/index.jsp";
    }

    @RequestMapping("/exit")
    public String exit(HttpSession session) {
        session.invalidate();
        return "redirect:/index.jsp";
    }

    @RequestMapping("/toMain")
    public String toMain() {
        return "main";
    }


    //查询全部
    @RequestMapping("/list/{pageNumber}")
    public String selectAllPage(@PathVariable(value = "pageNumber") Integer pageNumber,
                                @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize, ModelMap map,
                                @RequestParam(value = "valueId", required = false) Integer valueId) {
        PageInfo<User> pageInfo = userService.selectUserAll(pageNumber, pageSize);
        map.put("pageInfo", pageInfo);
        return "userList";
    }


    @RequestMapping("/addUI")
    public String addUI() {
        return "userAdd";
    }

    @RequestMapping("/insert")
    public String insert(User user) {
        userService.insert(user);
        return "redirect:/user/list/1";
    }

    @RequestMapping("/window")
    public String window() {
        return "window";
    }

}
